package com.weirong.smartvault.backservice.user;

import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.ConfirmLogRepository;
import com.weirong.smartvault.dao.DepartmentRepository;
import com.weirong.smartvault.entity.ConfirmLog;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.backservice.user.develop.UserRepository;
import com.weirong.smartvault.backservice.user.develop.User;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;

/**
 * 于2017/12/17由 KFEB4 创建 .
 * 用户通用接口
 */
public class IUser {
    private User user;
    private static UserRepository userRepository=SpringContextUtil.getBean(UserRepository.class);
    private static ConfirmLogRepository confirmLogRepository = SpringContextUtil.getBean(ConfirmLogRepository.class);

    public IUser(User user){
        this.user = user;
    }
    public static IUser getById(Integer id){
        return new IUser(userRepository.findOne(id));
    }
    public static Iterable<IUser> findAll() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        List<User> users = userRepository.findAll();
        return ListTool.cast(users,IUser.class);
    }
    public static IUser first(){
        return new IUser(userRepository.findAll().get(0));
    }
    public Integer getId() {
        return user.getId();
    }

    public static IUser getOne(Integer id) {
        return getById(id);
    }

    public String getUserName() {
        return user.getUserName();
    }

    public static IUser findOne(Integer id) {
        return getById(id);
    }

    public String getNickName(){
        return user.getUserName();
    }

    public String getDepartmentName(){
        return "";
    }

    public static IUser getNowUser(){
        //获取当前登陆的用户
        return IUser.first();
    }

    public ConfirmLog getNowConfirmLog() throws ParseException {
        //获取当前用户需要核账的核账记录
        List<ConfirmLog> logs = confirmLogRepository.findAllByConfirmTimeBetweenAndBranchId(DateUtil.getTodayStart(),DateUtil.getTodayEnd(),getDepartment().getId());
        if (logs.size()>0)
            return logs.get(logs.size()-1);
        return null;
    }

    public Department getDepartment() {
        DepartmentRepository departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
        return departmentRepository.findAll().get(0);
    }

}
